﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VBFOOL.MediaPlayer.Core.Interface;
using LibEBML.Common;
using System.Collections.ObjectModel;
namespace VBFOOL.MediaPlayer.MKV
{
    class SimpleSubtitleReader : ISubtitleReader
    {
        internal SimpleSubtitleReader(MKVFileSource source, TrackEntry track)
        {
            this.Track = track;
            //this.Document = document;
            this.Source = source;
            if (track.CodecPrivate != null)
            {
                m_Header = System.Text.Encoding.UTF8.GetString(track.CodecPrivate, 0, track.CodecPrivate.Length);
            }
            if (Track.ContentEncodings != null)
            {
                var ce = track.ContentEncodings[0];
                if (ce.Compression != null)
                {
                    CompressHead = ce.Compression.ContentCompSettings;
                }
            }
            m_Subtitles = new ObservableCollection<SubtitleItem>();
            m_SubList = new ReadOnlyObservableCollection<SubtitleItem>(m_Subtitles);
        }
        public TrackEntry Track { get; private set; }
        public MKVFileSource Source { get; private set; }
        byte[] CompressHead = null;
        internal void Enqueue(Cluster curcluster, SimpleBlock curblock)
        {
            long lp = (curcluster.TimeCode + curblock.TimeCode) * Source.TimeCodeScale;
            if (m_Subtitles.Any(m => m.TimeCode.Ticks == lp))
            {
                return;
            }
            long pc = 0;
            try
            {
                if (curblock is Block)
                {
                    var bs = curblock as Block;
                    if (bs.BlockDuration > 0)
                    {
                        pc = bs.BlockDuration * Source.TimeCodeScale;
                    }
                }
            }
            catch (Exception)
            {

                //throw;
            }
            try
            {
                var ds = curblock.GetBlockData(CompressHead);
                if (ds != null && ds.Count > 0)
                {
                    List<SubtitleItem> cs = new List<SubtitleItem>();
                    List<SubtitleItem> cs2 = new List<SubtitleItem>();
                    foreach (var item in ds)
                    {
                        SubtitleItem sub = new SubtitleItem()
                        {
                            TimeCode = TimeSpan.FromTicks(lp),
                            Duration = TimeSpan.FromTicks(pc),
                            Parent = this,
                            Text = System.Text.Encoding.UTF8.GetString(item, 0, item.Length)
                        };
                        m_Subtitles.Add(sub);
                        cs.Add(sub);
                    }
                    OnItemsChanged(cs, cs2);
                }
            }
            catch (Exception)
            {

                //throw;
            }

        }

        ObservableCollection<SubtitleItem> m_Subtitles = null;
        ReadOnlyObservableCollection<SubtitleItem> m_SubList = null;
        public IEnumerable<SubtitleItem> Items
        {
            get
            {
                return m_SubList;
            }
        }

        protected virtual void OnItemsChanged(IList<SubtitleItem> AddItems, IList<SubtitleItem> RemoveItems)
        {
            if (ItemsChanged != null)
            {
                ItemsChanged(this, new SubtitleChangedEventArgs(AddItems, RemoveItems));
            }
        }

        public event EventHandler<SubtitleChangedEventArgs> ItemsChanged;

        public string SubtitleType
        {
            get { return Track.CodecID; }
        }
        public string Name
        {
            get { return Track.Name ?? "(默认)"; }
        }
        string m_Header = "";
        public string SubtilteHeader
        {
            get { return m_Header; }
        }
    }
}
